<?php
declare (strict_types=1);

namespace app\common\model;

use ffhome\frame\model\BaseModel;
use ffhome\frame\service\SystemDeleteService;
use think\facade\Db;

class TourTemplateDetail extends BaseModel
{
    const MODEL_NAME = 'tour_template_detail';

    public static function getListByTemplateId($templateId)
    {
        $where = [['template_id', '=', $templateId],];
        return Db::name(self::MODEL_NAME)->withoutField('template_id')->where($where)
            ->order('sort', 'asc')->select()->toArray();
    }

    public static function insert($data)
    {
        Db::name(self::MODEL_NAME)->insert($data);
    }

    public static function update($id, $data)
    {
        Db::name(self::MODEL_NAME)->where('id', $id)->update($data);
    }

    public static function deleteByTemplateId($templateId, int $days, bool $recordDelete)
    {
        $where = [['template_id', 'in', $templateId],];
        if (!empty($days)) {
            $where[] = ['sort', '>', $days];
        }
        if ($recordDelete) {
            $row = Db::name(self::MODEL_NAME)->where($where)->select()->toArray();
            (new SystemDeleteService())->saveRecords($row, self::MODEL_NAME);
        }
        Db::name(self::MODEL_NAME)->where($where)->delete();
    }
}